package com.gmics.unifiq;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.os.Vibrator;
import android.util.Log;
import android.view.Menu;
import android.widget.Toast;

import com.gmics.android.R;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMapClickListener;
import com.google.android.gms.maps.GoogleMap.OnMapLongClickListener;
import com.google.android.gms.maps.GoogleMap.OnMarkerClickListener;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.Polyline;
import com.google.android.gms.maps.model.PolylineOptions;
import com.google.android.maps.GeoPoint;

public class CreateAvatar extends Activity implements OnMapClickListener,
		OnMapLongClickListener, OnMarkerClickListener {
	private GoogleMap avatarMap;
	private final LatLng SINGAPORE = new LatLng(1.368123, 103.798981);
	private final LatLng YISHUN = new LatLng(1.4295, 103.8352);
	private GeoPoint p;
	private Marker yishunMarker;
	private Geocoder gc;
	private int locationIcon = R.drawable.map_pin;
	private Polyline polyline;
	private PolylineOptions rectOptions;
	private boolean markerClicked;

	public List<Address> getAddress(LatLng point) {
		double latitude = point.latitude;
		double longitude = point.longitude;

		try {
			Geocoder geocoder;
			List<Address> addresses;
			geocoder = new Geocoder(getBaseContext());
			if (latitude != 0 || longitude != 0) {
				addresses = geocoder.getFromLocation(latitude, longitude, 1);
				String address = addresses.get(0).getAddressLine(0);
				String city = addresses.get(0).getAddressLine(1);
				Toast.makeText(getBaseContext(), city, Toast.LENGTH_LONG)
						.show();
				String country = addresses.get(0).getAddressLine(2);
				Log.d("TAG", "address = " + address + ", city =" + city
						+ ", country = " + country);
				return addresses;
			} else {
				Toast.makeText(getBaseContext(),
						"latitude and longitude are null", Toast.LENGTH_LONG)
						.show();
				return null;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.create_avatar_layout);

		avatarMap = ((MapFragment) getFragmentManager().findFragmentById(
				R.id.avatar_map)).getMap();
		avatarMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
		avatarMap.moveCamera(CameraUpdateFactory.newLatLngZoom(SINGAPORE, 12f));

		yishunMarker = avatarMap.addMarker(new MarkerOptions().position(YISHUN)
				.title("Yishun").snippet("Attended class reunion")
				.icon(BitmapDescriptorFactory.fromResource(locationIcon)));

		avatarMap.setOnMapClickListener(this);
		avatarMap.setOnMapLongClickListener(this);
		avatarMap.setOnMarkerClickListener(this);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public void onMapClick(LatLng point) {
		avatarMap.animateCamera(CameraUpdateFactory.newLatLng(point));
		markerClicked = false;
	}

	@Override
	public void onMapLongClick(LatLng point) {
		avatarMap.addMarker(new MarkerOptions().position(point).icon(
				BitmapDescriptorFactory.fromResource(locationIcon)));

		Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
		vibrator.vibrate(1000);

		markerClicked = false;
	}

	@Override
	public boolean onMarkerClick(Marker marker) {

		if (markerClicked) {

			if (polyline != null) {
				polyline.remove();
				polyline = null;
			}

			rectOptions.add(marker.getPosition());
			rectOptions.color(Color.RED);
			polyline = avatarMap.addPolyline(rectOptions);
		} else {
			if (polyline != null) {
				polyline.remove();
				polyline = null;
			}

			rectOptions = new PolylineOptions().add(marker.getPosition());
			markerClicked = true;
		}

		return true;
	}

}
